<?php

!defined('IN_ROOT') && exit('Access Denied');

class admodel {

    var $db;
    var $base;

    function admodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }


    function get($id) {
        $ad = array();
        $ad = $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."ad WHERE `advid`='$id'");
        $ad['fstarttime']=tdate($ad['starttime'],2,0);
        $ad['fendtime']=tdate($ad['endtime'],2,0);
        $ad['parameters'] = tstripslashes(unserialize($ad['parameters']));
        return $ad;
    }

    function get_list($start=0,$limit=10) {
        $adlist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."ad` ORDER BY `advid` desc limit $start,$limit");
        while($ad=$this->db->fetch_array($query)) {
            $ad['fstarttime']=tdate($ad['starttime'],2,0);
            $ad['fendtime']=tdate($ad['endtime'],2,0);
            $ad['parameters'] = tstripslashes(unserialize($ad['parameters']));
            $adlist[]=$ad;
        }
        return $adlist;
    }


    function add($title,$type,$parameters,$html='',$starttime='',$endtime='',$available=1) {
        $starttime && ($starttime=strtotime($starttime));
        $endtime && ($endtime=strtotime($endtime));
        $parameters = addslashes($parameters);
        $this->db->query('INSERT INTO '.DB_TABLEPRE."ad(`title`,`type`,`parameters`,`code`,`starttime`,`endtime`,`available`) values ('$title','$type','$parameters','$html','$starttime','$endtime',$available)");
        return $this->db->insert_id();
    }

    function update($title,$type,$parameters,$html,$starttime='',$endtime='',$id) {
        $starttime && ($starttime=strtotime($starttime));
        $endtime && ($endtime=strtotime($endtime));
        $parameters = addslashes($parameters);
        $this->db->query('update  '.DB_TABLEPRE."ad  set title='$title',type='$type',`parameters`='$parameters',`code`='$html',`starttime`='$starttime',`endtime`='$endtime' where `advid`=$id ");
    }

    function remove_by_id($ids) {
        $this->db->query("DELETE FROM `".DB_TABLEPRE."ad` WHERE `advid` IN ($ids)");
    }

    function create_html($ad) {
        $html = '';
        switch($ad['style']) {
            case 'text':
                $html = '<a href="'.$ad['texturl'].'" target="_blank" ) >'.$ad['textname'].'</a>';
                break;
            case 'image':
                $html = '<a href="'.$ad['imgadurl'].'" target="_blank"><img src="'.$ad['imgurl'].'"'.($ad['imageheight'] ? ' height="'.$ad['imageheight'].'"' : '').($ad['imagewidth'] ? ' width="'.$ad['imagewidth'].'"' : '').' border="0"></a>';
                break;
            case 'flash':
                $html = '<embed width="'.$ad['flashwidth'].'" height="'.$ad['flashheight'].'" src="'.$ad['flashurl'].'" type="application/x-shockwave-flash" wmode="transparent"></embed>';
                break;
                ;
                break;
            case 'code':
                $html = $ad['html'];
                break;
            default:
                break;
        }
        return $html;
    }
    function update_available($id,$available) {
        $this->db->query("UPDATE `".DB_TABLEPRE."ad` SET  `available` = '{$available}' WHERE `advid` = '{$id}'");
    }

    function get_by_type($adlist,$type) {
        $adsize = 0;
        $tadlist = array();
        if(empty($adlist))return 0;
        foreach($adlist as $ad) {
            if($ad['available'] && ($type == $ad['type'])) {
                $tadlist[] = $ad;
                $adsize++;
            }
        }
        $adsize && $index = intval(rand(0,($adsize-1)));
        return $tadlist[$index];
    }

}
?>
